package com.axx.android.profile;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.axx.android.LetterApp;
import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.axx.android.common.BaseModel;
import com.axx.android.common.Constants;
import com.axx.android.profile.auth.activity.LoginActivity;
import com.axx.android.setting.ModifyPassActivity;
import com.axx.android.utils.ContextUtils;
import com.axx.android.utils.GsonUtils;
import com.axx.android.utils.HttpUtils;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ModifyPwdActivity extends BaseActivity {


    @Bind(R.id.btn_back)
    Button btnBack;
    @Bind(R.id.layout_toolbar)
    RelativeLayout layoutToolbar;
    @Bind(R.id.et_old_pwd)
    EditText etOldPwd;
    @Bind(R.id.et_re_password)
    EditText etRePassword;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.et_new_pwd)
    EditText etNewPwd;

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    public void init(Bundle savedInstanceState) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etOldPwd.getText().toString())) {
            showToast("请输入旧密码");
            return;
        }
        if (TextUtils.isEmpty(etNewPwd.getText().toString())) {
            showToast("请输入新密码");
            return;
        }
        if (TextUtils.isEmpty(etRePassword.getText().toString())) {
            showToast("请重复输入新密码");

            return;
        }
        if (!TextUtils.equals(etNewPwd.getText().toString(), etRePassword.getText().toString())) {
            showToast("2次密码输入不一样");
            return;
        }
        showLoading("");
        Map<String, String> map = new HashMap<>();
        map.put("phone", LetterApp.authUser.getPhone());
        map.put("oldPsw", etOldPwd.getText().toString());
        map.put("newPsw", etNewPwd.getText().toString());
        map.put("token", LetterApp.authUser.getToken());
        HttpUtils.post(Constants.Url.Profile.PROFILE_UPDATE_PWD, map, new HttpUtils.SJCallBack() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                try {
                    BaseModel base = GsonUtils.fromJson(result, new TypeToken<BaseModel>() {
                    });
                    if (base.isSuccess()) {
                        showToast("密码修改成功，请重新登录");
                        finish();
                        LetterApp.authUser =null;
                        ContextUtils.clearUser(ModifyPwdActivity.this);
                        startActivity(new Intent(ModifyPwdActivity.this, LoginActivity.class));

                    }else{
                        showToast(base.message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showErrorParse();
                }
            }

            @Override
            public void onError() {
                hideLoading();
                showErrorNet();
            }
        });
    }
}
